package components.scanario
{
	import components.scanario.elements.c_scenarioElem;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	public class c_scenarioManager extends EventDispatcher {
		public static const ACTIONNOSCRIPTED: String = 'ACTIONNOSCRIPTED';
		private var _curIndex	: int;
		private var _list		: Array;
		private var _endDesc	: String;
		public function c_scenarioManager(a_endDesc: String='') {
			super();
			_list 		= new Array();
			_curIndex	= -1;
			_endDesc	= a_endDesc;
		}
		
		public function get endDesc(): String {
			return _endDesc;
		}
		
		public function addElement(a_element: c_scenarioElem): void {
			_list.push(a_element);
		}
		
		public function actionNoScripted(): void {
			dispatchEvent(new Event(ACTIONNOSCRIPTED));
		}
		
		public function nextElement(): void {
			if (curIndex == _list.length - 1)  dispatchEvent(new Event(Event.COMPLETE));
			else if (curIndex < _list.length) curIndex++;
		}
		
		public function get curIndex(): int {
			return _curIndex;
		}
		
		public function set curIndex(a_curIndex: int): void {
			if ((_curIndex != a_curIndex) && (a_curIndex < _list.length) && (a_curIndex > -2)) setCurIndex(a_curIndex);
		}

		protected function setCurIndex(a_curIndex: int): void {
			_curIndex = a_curIndex;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function get curElement(): c_scenarioElem {
			return (_curIndex > -1)?_list[_curIndex]:null;
		}
		
		public function dispose(): void {
			_list.forEach(function(item: c_scenarioElem, i: int, arr: Array): void {
				item.dispose();
			});
		}
	}
}